﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;

namespace Common
{
    public class Common
    {
        #region 添加
        //添加
        public static string ToAddSql<T>(T t, string TableName, Dictionary<string, bool> PKname) where T : class
        {
            string sql = "insert into {0} ({1}) values({2})";

            string filed = "";
            string value = "";

            string Pk = PKname.Keys.First();
            bool ifpk = PKname.Values.First();
            foreach (var item in t.GetType().GetProperties())
            {
                if (ifpk)
                {
                    if (item.Name == Pk)
                        continue;
                }
                
                    filed += item.Name + ",";

                    if (item.PropertyType == typeof(string) || item.PropertyType == typeof(DateTime))
                    {
                        value += "'" + item.GetValue(t) + "',";
                    }
                    else
                        if (item.PropertyType == typeof(int) || item.PropertyType == typeof(decimal))
                        {
                            value += item.GetValue(t) + ",";
                        }
                
            }

            filed = filed.TrimEnd(',');
            value = value.TrimEnd(',');

            return string.Format(sql, TableName, filed, value);
        }
        #endregion
        
        #region 显示模糊查询反填
        
        public static string GetList<T>(string TableName,params SqlParameter[] Parameter)
        {
            string sql = ""; 
            if (Parameter.Length!=0)
            {
                sql = "select * from {0} where 1=1 {1}";
                string where = "";
                Type type = typeof(T);
                foreach (var item in type.GetProperties())
                {
                    foreach (var p in Parameter)
                    {
                        if('@'+item.Name==p.ParameterName)
                        {
                            if(item.PropertyType==typeof(string))
                            {
                                where +=" and "+ item.Name + " like'%" + p.Value + "%'";
                            }else
                            {
                                where +=" and "+ item.Name + "=" + p.Value;                               
                            }
                        }
                    }
                }
                return string.Format(sql, TableName,where);
            }
            else
            {
                sql = "select * from {0} ";
                return string.Format(sql, TableName);
            }

           
        }


        #endregion

        #region 显示查询
        
        public static string GetTable<T>(string TableName,params SqlParameter[] Parameter)
        {
            string sql = ""; 
            if (Parameter.Length!=0)
            {
                sql = "select * from {0} where 1=1 {1}";
                string where = "";
                Type type = typeof(T);
                foreach (var item in type.GetProperties())
                {
                    foreach (var p in Parameter)
                    {
                        if('@'+item.Name==p.ParameterName)
                        {
                            if(item.PropertyType==typeof(string))
                            {
                                where +=" and "+ item.Name + "='" + p.Value + "'";
                            }else
                            {
                                where +=" and "+ item.Name + "=" + p.Value;                               
                            }
                        }
                    }
                }
                return string.Format(sql, TableName,where);
            }
            else
            {
                sql = "select * from {0} ";
                return string.Format(sql, TableName);
            }

           
        }


        #endregion

        #region 修改SQL语句
        /// <summary>
        /// 修改SQL语句
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <param name="tableName"></param>
        /// <param name="PKName"></param>
        /// <returns></returns>
        public static string ToUpdateSQL<T>(T t, string tableName, Dictionary<string, bool> PKName) where T : class
        {
            string SQL = "UPDATE {0} SET {1} WHERE {2}";

            string Pk = PKName.Keys.First();
            bool ifPK = PKName.Values.First();
            string SetValue = "";
            string Where = "";
            foreach (var item in t.GetType().GetProperties())
            {
                if (ifPK)
                {
                    if (item.Name == Pk)
                        continue;
                }
                if (item.PropertyType == typeof(string) || item.PropertyType == typeof(DateTime))
                {
                    SetValue += item.Name + " ='" + item.GetValue(t) + "',";
                }
                else if (item.PropertyType == typeof(int) || item.PropertyType == typeof(decimal))
                {
                    SetValue += item.Name + " =" + item.GetValue(t) + ",";
                }
            }
            SetValue = SetValue.TrimEnd(',');
            var Pkitem = t.GetType().GetProperty(Pk);
            if (Pkitem.PropertyType == typeof(string) || Pkitem.PropertyType == typeof(DateTime))
                Where = Pk + "= '" + Pkitem.GetValue(t) + "'";
            else
                Where = Pk + "=" + Pkitem.GetValue(t);
            return string.Format(SQL, tableName, SetValue, Where);
        }

        #endregion

        #region 删除SQL语句
        /// <summary>
        /// 删除SQL语句
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <param name="tableName"></param>
        /// <param name="PKName"></param>
        /// <returns></returns>
        public static string ToDeleteSQL<T>(T t, string tableName, Dictionary<string, bool> PKName) where T : class
        {
            string SQL = "DELETE FROM {0} WHERE {1}";

            string Pk = PKName.Keys.First();
            string Where = "";
            var Pkitem = t.GetType().GetProperty(Pk);
            if (Pkitem.PropertyType == typeof(string) || Pkitem.PropertyType == typeof(DateTime))
                Where = Pk + "= '" + Pkitem.GetValue(t) + "'";
            else
                Where = Pk + "=" + Pkitem.GetValue(t);
            return string.Format(SQL, tableName, Where);
        }

        #endregion
       
    }
}
